<?php
namespace Swoole\Registry;
use Swoole\Socket\IClient;
use Swoole\Socket\SocketClient;
use Swoole\Socket\Timer;
use Common\FileUtil;
use Common\Log;
class ThriftClientRegistry implements IClient{
    const THRIFT_SERVER = 1;
    const THRIFT_CLIENT = 2;
    private $client;
    private $config;
    private $host;
    private $port;
    // 注册中心地址
    private static $registryAddressList = array();
    // 可用thrift server地址
    private static $thriftServerAddressList = array();
//     private static $badAddressList = array();
    function __construct($config){
        $this->config = $config;
        $registry = $this->config['registry'];

        if(count($registry) > 0){
            foreach($registry as $address){
                $address = parse_url($address);
                self::$registryAddressList[] = array('host'=>$address['host'], 'port'=>$address['port']);
            }
            $registryServerCount = count(self::$registryAddressList);
            if($registryServerCount > 0){
                $registryServer = self::$registryAddressList[rand(0, $registryServerCount - 1)];
            }
            else{
                $registryServer = self::$registryAddressList[0];
            }
            //$registryServer = parse_url($registryServer);
            if(empty($registryServer) == true){
                exit("registry address is error");
            }
            $this->host = $registryServer['host'];
            $this->port = $registryServer['port'];
            $this->client = new SocketClient($this, $this->host, $this->port);
        }
        $timer = new Timer(3000, array($this, "timer") );
        $timer->start();
  
    }
    
    public function timer($id){
        $data = array(
            'type' => ThriftServerRegistry::THRIFT_CLIENT,
            'time' => time()
        );
        if ($this->client->isConnected() == false) {
            Log::info("connect retry");
            $this->client->connect($this->host, $this->port, - 1);
        } else {
            $send_data = json_encode($data) . "\r\n";
            $this->client->send($send_data);
        }
    }
    
    public function connect(){
        $this->client->connect();
//         Log::info($msg)
    }
    public function onConnect($cli){
        //$cli->send("GET / HTTP/1.1\r\n\r\n");
        echo "ThriftClientRegistry onConnect======\n";
        
        $data = array(
            'type' => ThriftServerRegistry::THRIFT_CLIENT,
            'time' => time()
        );
        $send_data = json_encode($data) . "\r\n";
        $cli->send($send_data);
    }
    public function onReceive($cli, $data){
        Log::info("ThriftClientRegistry onReceive===thrift servers:{$data}");
        $datas = json_decode($data, true);
        if(count($datas) > 0){
            
            $content = "<?php\n\r\t return " . var_export($datas) . ";\r\n?>";
            $filaPath = BASE_PATH . '/data/thrift_client_server.php';
            FileUtil::saveThriftServer($content, $filaPath);
        }
    }
    public function onClose($cli){
        Log::info("ThriftClientRegistry onClose======\n");
        
    }
    public function onError($cli){
        Log::info("ThriftClientRegistry onError======\n");
    }

    public function getAvailableServiceServer(){
        $filaPath = BASE_PATH . '/data/thrift_client_server.php';
        $servers = array();
        if(file_exists($filaPath) == true){
            $servers = include $filaPath;
        }
        return $servers;
//         $content = FileUtil::readThriftServer($filaPath);
//         $servers = json_decode($content, true);
    }


}

?>